package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class RegActivity extends AppCompatActivity implements View.OnClickListener {
    // 1.声明控件
    EditText nameEt,pwdEt,repwdEt;
    RadioGroup rg;
    RadioButton manRb,womanRb;
    CheckBox cb1,cb2,cb3,cb4;
    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        initView();
    }

    private void initView(){
        //2.找到布局当中的控件，进行初始化
        nameEt = findViewById(R.id.et_name);
        pwdEt = findViewById(R.id.et_pwd);
        repwdEt = findViewById(R.id.et_repwd);
        rg = findViewById(R.id.rg);
        manRb = findViewById(R.id.rb_man);
        womanRb = findViewById(R.id.rb_woman);
        cb1 = findViewById(R.id.cb1);
        cb2 = findViewById(R.id.cb2);
        cb3 = findViewById(R.id.cb3);
        cb4 = findViewById(R.id.cb4);
        btn = findViewById(R.id.btn_reg);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String name = nameEt.getText().toString();
        String pwd = pwdEt.getText().toString();
        String repwd = repwdEt.getText().toString();

        if (TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)||TextUtils.isEmpty(repwd)){
            Toast.makeText(this,"输入内容不能为空！",Toast.LENGTH_LONG).show();
            return;
        }

        if (!pwd.equals(repwd)){
            Toast.makeText(this,"两次输入密码不一致!!",Toast.LENGTH_LONG).show();
            return;
        }

        int checkedRadioButtonId = rg.getCheckedRadioButtonId();
        String gender = "男";
        if (checkedRadioButtonId==R.id.rb_man){
            gender = "男";
        }
        if (checkedRadioButtonId==R.id.rb_woman){
            gender = "女";
        }

        //获取多选按钮选中信息
        List<String> starList = new ArrayList<>();
        selectCbText(starList,cb1);
        selectCbText(starList,cb2);
        selectCbText(starList,cb3);
        selectCbText(starList,cb4);
        String msg = "用户名："+name+"，密码"+pwd+"，性别"+gender+",喜欢的明星:"+starList.toString();
        Log.i("animee",msg);
    }

    private  void selectCbText(List<String> starList,CheckBox cb1){
        if (cb1.isChecked()){
            starList.add(cb1.getText().toString());
        }else{
            starList.remove(cb1.getText().toString());
        }
    }
}
